<?php

/**
 * @author gray.guo
 */
namespace bc\util;

use yii;
use bc\model\baby\StatisticModel;

class Util
{
	
	/**
	 *
	 * @param Object $obj        	
	 */
	public static function objectToArray($obj)
	{
		$_array = is_object ( $obj ) ? get_object_vars ( $obj ) : $obj;
		
		foreach ( $_array as $key => $value )
		{
			$value = (is_array ( $value ) || is_object ( $value )) ? self::objectToArray ( $value ) : $value;
			$array [$key] = $value;
		}
		return $array;
	}
	
	/**
	 * 拼接当前host地址
	 *
	 * @param string $url        	
	 * @return string
	 */
	public static function baseUrl($url)
	{
		$url = ltrim ( $url, '/' );
		return Yii::$app->request->getHostInfo () . '/' . $url;
	}
	
	/**
	 *
	 * @param string $url        	
	 * @param array $params        	
	 */
	public static function httpGet($url, $params = null)
	{
		$ch = curl_init ();
		
		$str_params = '';
		
		if (! is_null ( $params ))
		{
			foreach ( $params as $key => $val )
			{
				$str_params .= $key . '=' . $val . '&';
			}
			
			$url .= '?' . rtrim ( $str_params, '&' );
		}
		
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		return $output;
	}
	/**
	 *
	 * @param string $url        	
	 * @param array $params        	
	 */
	public static function httpPost($url, $params = array())
	{
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		// post数据
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		// post的变量
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		return $output;
	}
	
	/**
	 * 
	 * @param array $arr
	 * @return string
	 */
	public static function jsonEncode($arr)
	{
		return json_encode($arr, JSON_UNESCAPED_UNICODE);
	}
	
	/**
	 * 以json数据post
	 * @param string $url
	 * @param string $json
	 */
	public static function jsonPost($url, $json)
	{
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		// post数据
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		// post的变量
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $json );
		
		//头信息使用json
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
				'Content-Type: application/json; charset=utf-8',
				'Content-Length: ' . strlen ( $json )
		) );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		return $output;
	}
	
	
	/**
	 * 获取访问设备的信息
	 */
	public static function getBroswer()
	{
		$agent = 'unknow';
		
		$httpUserAgent = $_SERVER['HTTP_USER_AGENT'];
		if($httpUserAgent)
		{
			if(strpos($httpUserAgent, 'MicroMessenger') > 0)
			{
				$agent = 'wechat';
			}
			elseif(strpos($httpUserAgent, 'Weibo') > 0)
			{
				$agent = 'weibo';
			}
		}
		
		return $agent;
	}

	/**
	 * 过滤emoji表情
	 * @param type $str
	 * @return type
	 */
	function replace_emoji($str){
		$tmpStr = json_encode($str); //暴露出unicode
		$tmpStr = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",$tmpStr); //将emoji的unicode留下，其他不动
		$text = json_decode($tmpStr);
		return $text;
	}

	function birthday($birthday){
		$age = strtotime($birthday);
		if($age === false){
			return false;
		}
		list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
		$now = strtotime("now");
		list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
		$age = $y2 - $y1;
		$m = ($m2-$m1)/12;
		$age += $m;
//    if((int)($m2.$d2) < (int)($m1.$d1))
//        $age -= 1;

		if($age<0)
			return 0;
//		elseif($age < 1)
//			return $age;
//
//		$age = $y2 - $y1;
//		if((int)($m2.$d2) < (int)($m1.$d1))
//	       $age -= 1;

		return $age;
	}

	//返回josn数据
	public static function  echoJosnData($msg,$error,$data=array())
	{
		$data=array(
			'msg'=>$msg,
			'error'=>$error,
			'data'=>$data
		);
		$json_data=json_encode($data,JSON_UNESCAPED_UNICODE);
		echo $json_data;
		die();
	}

	//f放回josnp数据
	public static function echoJosnpData($error=0,$data=array(),$msg='')
	{
		$echo_data=array(
			'error'=>$error,
			'message'=>$msg
		);

		foreach ($data as $key => $value) {
			$echo_data['data'][$key] = $value;
		}

		$json_data = json_encode($echo_data,JSON_UNESCAPED_UNICODE);
		$jsoncallback = htmlspecialchars($_REQUEST['jsoncallback']);
		echo $jsoncallback."(".$json_data.")";
		die();
	}

	//生成唯一的邀请码
	public static function create_guid($num = 8,$namespace = null)
	{
		static $guid = '';
		$uid = uniqid ( "", true );

		$data = $namespace;
		$data .= $_SERVER ['REQUEST_TIME'];     // 请求那一刻的时间戳
		$data .= $_SERVER ['HTTP_USER_AGENT'];  // 获取访问者在用什么操作系统
		$data .= $_SERVER ['SERVER_ADDR'];      // 服务器IP
		$data .= $_SERVER ['SERVER_PORT'];      // 端口号
		$data .= $_SERVER ['REMOTE_ADDR'];      // 远程IP
		$data .= $_SERVER ['REMOTE_PORT'];      // 端口信息

		$hash = strtoupper ( hash ( 'ripemd128', $uid . $guid . md5 ( $data ) ) );
		$guid = substr ( $hash, 0, $num );
		return $guid;
	}
}
